<?php
use Addons\QyWechat\Helper;

//根据站点  获取购买的插件列表
 function getAllModuleApi($params=array()){
	$data = [
		'webkey'=>$params['key'],
	];
	$secret = $params['secret'];

	$data['sign'] = makeSignApi($data,$secret);   //此接口用站点key和密钥 验证
	$url = 'https://www.kt8.cn/cloud/apiplugin/getAllModule';
	$resq = curlpostApi($url,$data);
	// dump($resq);
	return $resq;
}
//根据站点  获取购买的插件详情
 function getModuleDetailApi($params=array()){
	$data = [
		'webkey'=>$params['key'],
		'orderbh'=>$params['orderbh'],
	];
	$secret = $params['secret'];

	$data['sign'] = makeSignApi($data,$secret);   //此接口用站点key和密钥 验证
	$url = 'https://www.kt8.cn/cloud/apiplugin/getModuleDetail';
	$resq = curlpostApi($url,$data);
	// dump($resq);
	return $resq;
}
//下载插件
 function downModuleApi($params=array()){
	$data = [
		'webkey'=>$params['key'],
		'orderbh'=>$params['orderbh'],
	];
	$secret = $params['secret'];

	$data['sign'] = makeSignApi($data,$secret);   //此接口用站点key和密钥 验证
	$url = 'https://www.kt8.cn/cloud/apiplugin/downModule';
	$resq = curlpostApi($url,$data);
	// dump($resq);
	return $resq;
}

//获取签名
 function makeSignApi($data,$secret){
	ksort($data); //排序post参数
	reset($data); //内部指针指向数组中的第一个元素
	$signtext='';
	foreach ($data as $key => $val) { //遍历POST参数
	    if ($val == '' || $key == 'sign') continue; //跳过这些不签名
	    if ($signtext) $signtext .= '&'; //第一个字符串签名不加& 其他加&连接起来参数
	    $signtext .= "$key=$val"; //拼接为url参数形式
	}
	$sign=md5($signtext.$secret);
	return $sign;
}

 function curlpostApi($url,$data=array(),$header=null){
	$post_data = http_build_query($data);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    if(!empty($header)){
    	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    $output = curl_exec($ch);
    // if($output === false){
    // 	dump(curl_error($ch));
    // }
    curl_close($ch);
    return json_decode($output,1);
}
